#pragma once

#include <iostream>
#include <string>
#include <cstring>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "protocol.hpp"

#define NUM 1024

class CalClient
{
public:
    CalClient(const std::string &serverip, const uint16_t &serverport)
        : _sock(-1), _serverip(serverip), _serverport(serverport)
    {
    }
    void initClient()
    {
        // 1. 创建socket
        _sock = socket(AF_INET, SOCK_STREAM, 0);
        if (_sock < 0)
        {
            std::cerr << "socket create error" << std::endl;
            exit(2);
        }
        // 2. tcp的客户端要不要bind？要的！ 要不要显示的bind？不要！这里尤其是client port要让OS自定随机指定！
        // 3. 要不要listen？不要！
        // 4. 要不要accept? 不要！
        // 5. 要什么呢？？要发起链接！
    }
    void start()
    {
        struct sockaddr_in server;
        memset(&server, 0, sizeof(server));
        server.sin_family = AF_INET;
        server.sin_port = htons(_serverport);
        server.sin_addr.s_addr = inet_addr(_serverip.c_str());

        if (connect(_sock, (struct sockaddr *)&server, sizeof(server)) != 0)
        {
            std::cerr << "socket connect error" << std::endl;
        }
        else
        {
            std::string line;
            std::string inbuffer;
            while (true)
            {
                // 发送业务
                std::cout << "mycal>>> ";
                std::getline(std::cin, line); // 1+1
                std::cout<<"命令行输入："<<line<<std::endl;

                Request req=ParseLine(line);
                std::string req_str;
                req.serialize(&req_str);
                std::string req_pac_str=enLength(req_str);
                send(_sock, req_pac_str.c_str(), req_pac_str.size(), 0); 

                //std::string send_string = "5\r\n1 + 2\r\n5\r\n1 / 0\r\n5\r\n1 % 2\r\n";
                //send(_sock, send_string.c_str(), send_string.size(), 0); 

                // 接收业务
                std::string recv_pac;
                recvPackage(_sock, inbuffer, &recv_pac);
                std::string pac_text;
                deLength(recv_pac, &pac_text);
                Response resp;
                resp.deserialize(pac_text);
                std::cout << "exitCode: " << resp._exitcode << std::endl;
                std::cout << "result: " << resp._result << std::endl;
            }
        }
    }
    Request ParseLine(const std::string &line)
    {
        std::cout<<"命令行输入："<<line<<std::endl;
        // 命令行输入的数据进行处理成对应结构体格式
        // 把输入的line当作一串字符，挨个处理，判断是不是数字
        int status = 0; // 0：碰到操作符之前 1：碰到操作符 2：碰到操作符之后
        int i = 0;
        std::string left_str, right_str;
        char op;
        int cnt = line.size();
        while (i < cnt)
        {
            switch (status)
            {
            case 0:
            {
                if (!isdigit(line[i]))
                {
                    status = 1;
                    op = line[i];
                }
                else
                {
                    left_str.push_back(line[i++]);
                }
            }
            break;
            case 1:
                i++;
                status=2;
                break;
            case 2:
                right_str.push_back(line[i++]);
                break;
            }
        }
        std::cout << std::stoi(left_str) << " " << std::stoi(right_str) << " " << op << std::endl;

        return Request(std::stoi(left_str), std::stoi(right_str), op);
    }
    ~CalClient()
    {
        if (_sock >= 0)
            close(_sock);
    }

private:
    int _sock;
    std::string _serverip;
    uint16_t _serverport;
};
